!function() {
	var from = layui.form(),
		$ = layui.jquery,
		layer = layui.layer,
		laytpl = layui.laytpl,
		laydate = layui.laydate;
	$("#loading").hide().remove();
	var desktopappTmp = ['<div class="desktop-app" data-id="{{d.apps[app].appid}} " data-title="{{d.apps[app].name}}" data-url="{{d.apps[app].url}}" data-icon="{{d.apps[app].icon}}"  data-isicon="{{d.apps[app].isicon}}" data-height="{{d.apps[app].height}}" data-width="{{d.apps[app].width}}" data-fid="{{app}}">', '<i class="iconfont {{d.apps[app].icon}} {{d.apps[app].icon}}-bg"></i>', '<span class="desktop-title layui-elip">{{d.apps[app].name}}</span>', "</div>"].join(""),
		desktopTmp = ["{{# layui.each(d.menu, function(index, menuitem){ if(index>=3)return false;}}", '<div class="swiper-slide">', '<div class="desktopContainer"  data-menuid="{{menuitem.menuid}}" data-name="{{menuitem.name}}" >', "{{# layui.each(menuitem.app, function(index, app){}}", desktopappTmp, "{{# });}}", "</div>", "</div>", "{{# }); }} "].join(""),
		desktopOpeningTmp = ["{{# layui.each(d.menu[3].app, function(index, app){}}", desktopappTmp, "{{# });}}"].join("");
	laytpl(desktopTmp).render(desktpData, function(html) {
		$(".swiper-wrapper").html(html)
	});
	laytpl(desktopOpeningTmp).render(desktpData, function(html) {
		$(".opening-menu-app-list").html(html)
	});
	$(".desktop-container").css("height", $(window).height() - 30);
	$(window).resize(function(event) {
		$(".desktop-container").css("height", $(window).height() - 40);
		$(".desktopContainer").css("height", $(".desktop-container").height());
		arrange()
	});
	var swiper = new Swiper(".swiper-container", {
		pagination: ".swiper-pagination",
		simulateTouch: false,
		slidesPerView: 1,
		paginationClickable: true,
		spaceBetween: 30,
		keyboardControl: true,
		mousewheelControl: true,
		onSlideChangeEnd: function(swiper) {
			arrange(swiper.realIndex)
		}
	});
	$(".desktopContainer").sortable({
		revert: true
	});
	$(".desktopContainer").sortable({
		connectToSortable: ".desktopContainer",
		stop: function(event, ui) {
			arrange()
		}
	}).disableSelection();
	$(".desktopContainer").on("click", function() {
		$(".desktop-menu").hide()
	});
	arrange();

	function arrange(index) {
		index = $(".swiper-slide-active").index();
		index = index == "" || index == undefined ? 0 : index;
		var desktopContainer = $(".desktopContainer:eq(" + index + ")");
		var working = $(".desktopContainer");
		var position = {
			x: 0,
			y: 0,
			bottom: 65,
			width: 96,
			height: 96,
			parent: {
				height: 0,
				width: 0
			},
			padding: {
				top: 10,
				left: 10,
				right: 0,
				bottom: 10
			}
		};
		position.parent.height = working.height() - 40;
		position.parent.width = working.width();
		desktopContainer.find(".desktop-app").each(function(index, elem) {
			$(elem).css("top", position.y + "px");
			$(elem).css("left", position.x + "px");
			position.height = $(elem).height();
			position.width = $(elem).width();
			position.y = position.y + position.height + position.padding.bottom + position.padding.bottom;
			if (position.y >= position.parent.height - position.bottom) {
				position.y = 0;
				position.x = position.x + position.width + position.padding.left
			}
		})
	}
	var hide = function() {
			layer.closeAll("tips")
		},
		stope = function(e) {
			e = e || window.event;
			e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true
		},
		appopen = function(data, appthat) {
			var isopen = true;
			$(document).find(".taskbar-app").each(function(index, element) {
				if ($(element).attr("title") == data.title) {
					appthat.removeClass("disabled");
					$(element).click();
					isopen = false;
					return
				}
			});
			if (!isopen) {
				return
			}
			var count = $(".taskbar-app").length + 1,
				maxcount = parseInt((layui.jquery(".desktop-taskbar").width() - 160) / 110);
			if (count > maxcount) {
				layer.alert("您打开的窗口太多,请先关闭一些窗口!", {
					title: "官人休息下?",
					icon: 2,
					zIndex: layer.zIndex + 1
				});
				return
			}
			var width = data.width ? data.width : $(".desktop-container").width() * 0.75,
				height = data.height ? data.height : $(".desktop-container").height() * 0.85;
			var taskbarid = "";
			layer.open({
				type: 2,
				title: [data.title, "background-color:#515E6B;color:#fff"],
				shadeClose: true,
				shade: false,
				maxmin: true,
				area: [width + "px", height + "px"],
				shift: -1,
				content: data.url,
				zIndex: layer.zIndex,
				skin: "desktop-win-app",
				success: function(layero, index) {
					appthat.removeClass("disabled");
					layer.setTop(layero);
					taskbarid = "taskbar-" + $(layero).attr("id");
					var taskbarHtml = "";
					if (data.isicon) {
						taskbarHtml = ['<div class="layui-inline layui-elip taskbar-app taskbar-app-on" title="' + data.title + '" id="' + taskbarid + '"><i class="iconfont ' + data.icon + " " + data.icon + '-bg"></i><span class="desktop-title layui-elip">' + data.title + "</span></div>"].join("")
					} else {
						taskbarHtml = ['<div class="layui-inline layui-elip taskbar-app taskbar-app-on" title="' + data.title + '" id="' + taskbarid + '"><span class="desktop-title layui-elip">' + data.title + "</span></div>"].join("")
					}
					if ($("#" + taskbarid).is(":visible")) {
						return
					}
					$(".desktop-taskbar-app-list").append(taskbarHtml);
					$("#" + taskbarid).on("click", function() {
						var that = $(this);
						if (that.hasClass("taskbar-app-on")) {
							$(layero).find(".layui-layer-setwin .layui-layer-min").click()
						} else {
							that.addClass("taskbar-app-on").siblings().removeClass("taskbar-app-on");
							$(layero).show();
							layer.zIndex = parseInt(layer.zIndex + 1);
							layer.style(index, {
								zIndex: layer.zIndex
							})
						}
					}).siblings().removeClass("taskbar-app-on")
				},
				min: function(layero, index) {
					$(layero).hide();
					$("#" + taskbarid).removeClass("taskbar-app-on");
					var zindexall = [];
					$(document).find(".layui-layer-iframe:visible").each(function(index, element) {
						zindexall.push($(element).css("z-index"))
					});
					if (zindexall.length < 1) {
						return false
					}
					var bigzindex = zindexall.sort().pop();
					$(document).find(".layui-layer-iframe:visible").each(function(index, element) {
						if ($(element).css("z-index") == bigzindex) {
							$("#taskbar-" + $(element).attr("id")).addClass("taskbar-app-on");
							return false
						}
					});
					return false
				},
				full: function(layero, index) {},
				restore: function(layero, index) {},
				moveEnd: function() {
					$("#" + taskbarid).addClass("taskbar-app-on").siblings().removeClass("taskbar-app-on")
				},
				cancel: function(index) {
					var alldesktopapp = layui.data("desktop-app")["desktop-app-" + index];
					layui.each(alldesktopapp, function(index, itme) {
						layer.close(itme)
					});
					layui.data("desktop-app", {
						key: "desktop-app-" + index,
						remove: true
					});
					$("#" + taskbarid).remove()
				},
				end: function() {
					appthat.removeClass("disabled");
					$("#" + taskbarid).remove()
				}
			})
		},
		desktopContextmenu = function(othis, dom) {
			layer.tips(dom, this, {
				tips: 1,
				time: 0,
				shift: 5,
				fix: true,
				skin: "layui-box layui-layim-contextmenu",
				success: function(layero) {
					var stopmp = function(e) {
							stope(e)
						};
					layero.off("mousedown", stopmp).on("mousedown", stopmp)
				}
			});
			$(document).off("mousedown", hide).on("mousedown", hide);
			$(window).off("resize", hide).on("resize", hide)
		},
		hidedesktopmenu = function() {
			$(".desktop-menu").hide()
		},
		pattern = function(fmt) {
			var date = new Date();
			var o = {
				"M+": date.getMonth() + 1,
				"d+": date.getDate(),
				"h+": date.getHours() % 12 == 0 ? 12 : date.getHours() % 12,
				"H+": date.getHours(),
				"m+": date.getMinutes(),
				"s+": date.getSeconds(),
				"q+": Math.floor((date.getMonth() + 3) / 3),
				"S": date.getMilliseconds()
			};
			var week = {
				"0": "\u65e5",
				"1": "\u4e00",
				"2": "\u4e8c",
				"3": "\u4e09",
				"4": "\u56db",
				"5": "\u4e94",
				"6": "\u516d"
			};
			if (/(y+)/.test(fmt)) {
				fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
			}
			if (/(E+)/.test(fmt)) {
				fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? "\u661f\u671f" : "\u5468") : "") + week[date.getDay() + ""])
			}
			for (var k in o) {
				if (new RegExp("(" + k + ")").test(fmt)) {
					fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
				}
			}
			return fmt
		},
		getweek = function(day) {
			var week = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
			return week[day]
		},
		refreshtime = function() {
			$(".taskbar-time").attr("title", pattern("yyyy年MM月dd日 EEE"));
			$("#laydate-hs").text(pattern("HH:mm"));
			$("#laydate-ymd").text(pattern("yyyy/MM/dd"))
		},
		hide_opening_menu = function() {
			$("#opening-menu").removeClass("opening-menu-on")
		};
	setInterval(refreshtime, 1000);
	$(document).contextmenu(function() {
		return false
	});
	$(".desktopContainer").on("contextmenu", function(event) {
		var x = event.clientX,
			y = event.clientY,
			desktopmenu = $(".desktop-menu");
		var width = document.body.clientWidth,
			height = document.body.clientHeight;
		x = (x + desktopmenu.width()) >= width ? width - desktopmenu.width() - 15 : x;
		y = (y + desktopmenu.height()) >= height - 40 ? height - desktopmenu.height() - 15 : y;
		desktopmenu.css({
			"top": y,
			"left": x
		}).show()
	});
	$(".desktop-app").on("click", function() {
		if ($(this).hasClass("disabled")) {
			return false
		}
		$(this).addClass("disabled");
		appopen($(this).data(), $(this))
	});
	$("#showdesktop,.taskbar-showdesktop").on("click", function() {
		$(".desktop-menu").hide();
		$(document).find(".layui-layer .layui-layer-min").click();
		$(document).find(".taskbar-app").removeClass("taskbar-app-on")
	});
	$("#closeall").on("click", function() {
		var count = $(".taskbar-app").length;
		$(".desktop-menu").hide();
		if (count < 1) {
			return
		}
		layer.alert("确定关闭所有窗口?", {
			icon: 0,
			btn: ["确定", "取消"],
			zIndex: parseInt(layer.zIndex + 1),
			yes: function(index, layero) {
				$(document).find(".taskbar-app").remove();
				layer.closeAll("iframe");
				layer.close(index)
			},
			end: function() {}
		})
	});
	$("#lockscreen").on("click", function() {
		$(".desktop-menu").hide();
		layer.open({
			type: 1,
			title: false,
			closeBtn: false,
			area: "300px;",
			shade: 0.8,
			id: "LAY_layuipro",
			resize: false,
			btn: ["解锁"],
			btnAlign: "c",
			moveType: 1,
			content: '<div style="padding: 50px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">已锁屏</div>',
			success: function(layero) {
				var btn = layero.find(".layui-layer-btn")
			}
		})
	});
	$("#systemsettings").on("click", function() {
		$(".desktop-menu").hide();
		appopen({
			"title": "系统设置",
			"url": "set.html",
			"icon": "icon-xitongshezhi",
			"width": "1000",
			"height": "500"
		})
	});
	$("#themesettings").on("click", function() {
		$(".desktop-menu").hide();
		var themebox = $("#theme_body").html();
		$("#theme_body").css("display", "block");
		layer.open({
			type: 1,
			title: "主题设置",
			area: ["810px", "370px"],
			skin: "layui-layer-theme",
			anim: 2,
			shade: false,
			zindex: 999,
			content: themebox,
			success: function(layero, index) {
				$("#theme_area a").click(function() {
					var a = $(this);
					var themeid = a.attr("themeid");
					var nowid = a.attr("nowid");
					var src = a.attr("bigpicurl");
					$("body").css("background-image", "url(" + src + ")");
					$("#themezoom").css("background-image", "url(" + src + ")");
					$("#theme_area a").removeClass("theme_select");
					a.addClass("theme_select");
					$.post("inc.theme.asp", {
						id: nowid
					}, function(data) {
						if (data == "success") {
							layer.msg("主题保存成功")
						} else {
							layer.msg("主题保存失败")
						}
					})
				});
				$("#theme_area .theme_upload").click(function() {
					$(".desktop-menu").hide();
					appopen({
						"title": "自定义主题",
						"url": "themesave.asp",
						"icon": "icon-zhichi",
						"width": "650",
						"height": "480"
					})
				})
			}
		})
	});
	$("#technicalsupport").on("click", function() {
		$(".desktop-menu").hide();
		appopen({
			"title": "技术支持",
			"url": "support.html",
			"icon": "icon-zhichi",
			"width": "632",
			"height": "408"
		})
	});
	$(".logout").on("click", function() {
		$(".desktop-menu").hide();
		layer.alert("确定要注销登录吗?", {
			icon: 0,
			btn: ["确定", "取消"],
			zIndex: parseInt(layer.zIndex + 1000),
			yes: function(index, layero) {
				$.post("logout.asp", {}, function(data) {
					if (data == "success") {
						layer.msg("注销成功");
						window.location.replace("/")
					} else {
						layer.msg("操作失败")
					}
				})
			},
			end: function() {}
		})
	});
	$(".taskbar-win").on("click", function() {
		$("#opening-menu").toggleClass("opening-menu-on").off("mousedown", stope).on("mousedown", stope);
		$(document).off("mousedown", hide_opening_menu).on("mousedown", hide_opening_menu);
		$(window).off("resize", hide_opening_menu).on("resize", hide_opening_menu);
		$(this).off("mousedown", stope).on("mousedown", stope)
	});
	
	$(".metro li.startm").on("click", function() {
		$("#opening-menu").removeClass("opening-menu-on");
		var that = $(this),
			cmd = that.attr("cmd");
		var url = $(this).attr("url");
		var title = $(this).attr("title");
		var width = $(this).data("width") ? $(this).data("width") : document.body.clientWidth * 0.75;
		var height = $(this).data("height") ? $(this).data("height") : document.body.clientHeight * 0.75;
		switch (cmd) {
		case cmd:
			appopen({
				"title": title,
				"url": url,
				"icon": "icon-xitongshezhi",
				"isicon": "1",
				"width": width,
				"height": height
			}, that);
			break;
		default:
		}
	});
	var ytc = 0;

	function tanchuxiaoxi() {
		layer.open({
			type: 2,
			title: "内部信息提醒",
			skin: "layui-layer-xiaoxi",
			shade: false,
			shadeClose: false,
			zIndex: 1080,
			area: ["60%", "70%"],
			content: "/crm/message.asp",
			success: function(index, layero) {
				ytc = 1;
				return (ytc)
			},
			end: function(index, layero) {
				ytc = 0;
				return (ytc)
			}
		})
	}
	if (desk_message > 0) {
		setTimeout(tanchuxiaoxi(), 7000)
	}

	if (desk_xitong == 1) {
		matop = "15px";
		maleft = (document.body.clientWidth - 300 - 325) + "px"
	} else {
		matop = "15px";
		maleft = (document.body.clientWidth - 300 - 15) + "px"
	}
	bianqian = $(".bianqian").text();
	if (desk_bianqian == 1) {
		setTimeout(function() {
			layer.open({
				type: 1,
				title: "桌面便签",
				area: ["300px", "170px"],
				skin: "layui-layer-notepaper",
				offset: [matop, maleft],
				anim: 6,
				shade: false,
				zIndex: 999,
				content: '<textarea class="layui-textarea notepaper">' + bianqian + "</textarea>",
				success: function(layero, index) {
					$(layero).find(".notepaper").on("change", function() {
						$.post("inc.bianqian.asp", {
							bq_content: escape($(this).val())
						}, function(data) {
							if (data == "success") {
								layer.msg("便签保存成功")
							} else {
								layer.msg("便签保存失败")
							}
						})
					})
				}
			})
		}, 1.2 * 1000)
	}
	if (desk_xitong == 1) {
		setTimeout(function() {
			layer.open({
				type: 1,
				title: "系统提醒",
				area: ["300px", "300px"],
				skin: "layui-layer-notice",
				offset: ["15px", (document.body.clientWidth - 300 - 15) + "px"],
				shade: false,
				zIndex: 999,
				content: '<iframe id="noticek" style="height:260px;" width="100%" frameborder=0 scrolling="auto" src="notice.html" allowTransparency="true"></iframe>',
				success: function(layero, index) {},
				resizing: function(layero) {
					$("#noticek").height(($(".layui-layer-notice .layui-layer-content").height() - 5) + "px")
				}
			})
		}, 1.4 * 1000)
	}
	

    //不显示执行过程
    var yc_left=document.body.clientWidth+10
	
	setTimeout(function() {
            layer.open({
				type: 1,
				title: "自动执行程序",
				area: ["300px", "300px"],
				skin: "layui-layer-notice",
				offset: ["330px",yc_left + "px"],
				shade: false,
				zIndex: 999,
				content: '<iframe style="height:260px;" width="100%" frameborder=0 scrolling="auto" src="auto/?LaiYuan=desk" allowTransparency="true"></iframe>'
			})
	 }, 1.7 * 1000)

	 
	
	
}();